﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite
{
    public partial class CList : PreBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCompany();
                BindHotCompany();
                
                BindBlog();
                BindPVOrder();
                BindAd();//加载广告
            }
        }

        #region 加载小组
        private void BindCompany()
        {
            //设置控件属性，从BasePage中得到
            PageConfig(CPager);

            //List<Member> list = new List<Member();
            CompanysHelper chelper=new CompanysHelper();
            Criteria c = new Criteria();

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);
            string order = Request.QueryString["o"] + "";

            CPager.ItemCount = chelper.QueryCompanysCountBySearch(c);

            // 以下的值从BasePage中得到
            CPager.UrlFormat = UrlFormat;
            //CPager.PrefixText = GetPreFixText(CPager);


            string[] fields = new string[] { "ID", "CompanyName", "RealmName", "Brief", "BlogPVNum", "SmallHeadImage", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            if (order == "")
            {
                dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, CPager.Begin - 1, CPager.Count, fields);
            }
            else if (order == "pv")
            {
                dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, CPager.Begin - 1, CPager.Count, fields);
            }
            else if (order == "date")
            {
                dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, CPager.Begin - 1, CPager.Count, fields);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string smallheadimage = dr["SmallHeadImage"] + "";
                    if (string.IsNullOrEmpty(smallheadimage))
                    {
                        dr["SmallHeadImage"] = "images/xiaozuphoto.jpg";
                    }
                }
            }

            dlCompanyList.DataSource = dt;
            dlCompanyList.DataBind();
        }
        #endregion

        #region 加载热门小组
        private void BindHotCompany()
        {
            CompanysHelper chelper = new CompanysHelper();
            Criteria c = new Criteria();

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.MoreThan, "BlogPVNum", 0);

            string[] fields = new string[] { "ID", "CompanyName", "RealmName", "Brief", "BlogPVNum", "SmallHeadImage", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, 0, 6, fields);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string smallheadimage = dr["SmallHeadImage"] + "";
                    if (string.IsNullOrEmpty(smallheadimage))
                    {
                        dr["SmallHeadImage"] = "images/xiaozuphoto.jpg";
                    }
                }
            }

            dlHotCompanyList.DataSource = dt;
            dlHotCompanyList.DataBind();
        }
        #endregion

        #region 加载广告
        private void BindAd()
        {
            AdPositionHelper adhelper = new AdPositionHelper();
            litAd34.Text = adhelper.BindSystemAd("34", 1);
        }
        #endregion

        #region 加载小组最新创作
        private void BindBlog()
        {
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.NotEquals, "CompanyID", "");
            c.Add(CriteriaType.NotEquals, "CompanyID", "0");

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 15, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtpvBlog.Columns.Add(dcTitle);
            dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
            dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtpvBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 26);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }
            rptBlog.DataSource = dtpvBlog;
            rptBlog.DataBind();
        }
        #endregion

        #region 加载小组内PV排行榜
        private void BindPVOrder()
        {
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.NotEquals, "CompanyID", "");
            c.Add(CriteriaType.NotEquals, "CompanyID", "0");

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("PVCount", OrderMode.Desc) }, 0, 15, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtpvBlog.Columns.Add(dcTitle);
            dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
            dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtpvBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 26);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }
            rptPVOrder.DataSource = dtpvBlog;
            rptPVOrder.DataBind();
        }
        #endregion
    }
}